Cotonti Database Connection class.
A compact extension to standard PHP PDO class with slight Cotonti-specific needs,
handy functions and query builder.
1) If called with one parameter:
Works like PDO::query()
Executes an SQL statement in a single function call, returning the result set (if any) returned by the statement as a PDOStatement object.
parameters - Associative or numeric array of parameters
Returns:
Array with placeholders substituted
system\database.php at line 183
_startTimer
private void _startTimer()
Starts query execution timer
system\database.php at line 196
_stopTimer
private void _stopTimer(mixed query)
Stops query execution timer
system\database.php at line 225
countRows
public int countRows(string table_name)
Returns total number of records contained in a table
Parameters:
table_name - Table name
Returns:
system\database.php at line 238
delete
public int delete(string table_name, string condition, array parameters)
Performs simple SQL DELETE query and returns number of removed items.
Parameters:
table_name - Table name
condition - Body of WHERE clause
parameters - Array of statement input parameters, see http://www.php.net/manual/en/pdostatement.execute.php
Returns:
Number of records removed on success or FALSE on error
system\database.php at line 285
fieldExists
public bool fieldExists(string table_name, string field_name)
Checks if a field exists in a table
Parameters:
table_name - Table name
field_name - Field name
Returns:
TRUE if the field exists, FALSE otherwise
system\database.php at line 298
indexExists
public bool indexExists(string table_name, string index_name, mixed index_columns)
Checks if an index with the same index name or column order exists
Parameters:
table_name - Table name
index_name - Index/Key name
index_columns - Either a string for a single column name or an array for single/multiple columns. No column check will be preformed if left empty.
Returns:
TRUE if the index name or column order exists, FALSE otherwise
system\database.php at line 339
addIndex
public int addIndex(string table_name, string index_name, mixed Either, mixed index_columns)
Adds an index on a table
Parameters:
table_name - Table name
index_name - Index/Key name
Either - a string for a single column name or an array for single/multiple columns. $index_name will be used if empty.
Returns:
Number of rows affected
system\database.php at line 369
insert
public int insert(string table_name, array data, bool insert_null, bool ignore, array update_fields)
Performs SQL INSERT on simple data array. Array keys must match table keys, optionally you can specify
key prefix as third parameter. Strings get quoted and escaped automatically.
Ints and floats must be typecasted.
You can use special values in the array:
- PHP NULL => SQL NULL
- 'NOW()' => SQL NOW()
Performs single row INSERT if $data is an associative array,
performs multi-row INSERT if $data is a 2D array (numeric => assoc)
Parameters:
table_name - Table name
data - Associative or 2D array containing data for insertion.
insert_null - Insert SQL NULL for empty values rather than ignoring them.
ignore - Ignore duplicate key errors on insert
update_fields - List of fields to be updated with ON DUPLICATE KEY UPDATE
Returns:
The number of affected records
system\database.php at line 473
prep
public string prep(string str)
Prepares a param for use in SQL query without wrapping it with quotes
Parameters:
str - Param string
Returns:
Escaped param
system\database.php at line 482
registerTable
public void registerTable(string table_name)
Registers an unprefixed table name in table names registry
Parameters:
table_name - Table name without a prefix, e.g. 'pages'
system\database.php at line 498
runScript
public string runScript(string script, resource conn)
Runs an SQL script containing multiple queries.
Parameters:
script - SQL script body, containing formatted queries separated by semicolons and newlines
conn - Custom connection handle
Returns:
Error message if an error occurs or empty string on success
system\database.php at line 551
query
public PDOStatement query(string query, array parameters)
1) If called with one parameter:
Works like PDO::query()
Executes an SQL statement in a single function call, returning the result set (if any) returned by the statement as a PDOStatement object.
2) If called with second parameter as array of input parameter bindings:
Works like PDO::prepare()->execute()
Prepares an SQL statement and executes it.
parameters - An array of values to be binded as input parameters to the query. PHP int parameters will beconsidered as PDO::PARAM_INT, others as PDO::PARAM_STR.
Returns:
system\database.php at line 607
update
public int update(string table_name, array data, string condition, array parameters, bool update_null)
Performs SQL UPDATE with simple data array. Array keys must match table keys, optionally you can specify
key prefix as fourth parameter. Strings get quoted and escaped automatically.
Ints and floats must be typecasted.
You can use special values in the array:
- PHP NULL => SQL NULL
- 'NOW()' => SQL NOW()
Parameters:
table_name - Table name
data - Associative array containing data for update
condition - Body of SQL WHERE clause
parameters - Array of statement input parameters, see http://www.php.net/manual/en/pdostatement.execute.php
update_null - Nullify cells which have null values in the array. By default they are skipped